package com.juyu.miliao.api.controller;


import com.juyu.miliao.api.constant.UserTypeEnum;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.vo.*;
import com.juyu.miliao.api.service.EavesdroppingService;
import com.juyu.miliao.api.service.OfficialQuestionInfoService;
import com.juyu.miliao.api.service.OfficialQuestionToAnswerService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 偷听控制器
 *
 * @author xuanqicheng
 * @create 2018-14-2:04 PM
 */
@RestController
@RequestMapping("/eavesdropping")
public class EavesdroppingController {

    @Autowired
    private EavesdroppingService eavesdroppingService;

    @Autowired
    private OfficialQuestionInfoService officialQuestionInfoService;

    @Autowired
    private OfficialQuestionToAnswerService officialQuestionToAnswerService;

    @Autowired
    private UserInfoCache userInfoCache;


    /**
     * vip秘密小屋
     *
     * @param token
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/vip/voice/list/{page}-{pageSize}")
    public Object getVipVoiceList(Authentication token, @PathVariable("page") Integer page,
                                  @PathVariable("pageSize") Integer pageSize) {

        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }


        VipVoiceListVo vipVoiceListVo = new VipVoiceListVo();


        //获取官方信息
        OfficialQuestionVo obj = officialQuestionInfoService.getOneOfficalQuestion();

        vipVoiceListVo.setOfficial(obj);

        List<QuestionAndAnswerVo> list = eavesdroppingService.getVipVoiceList(page, pageSize);

        vipVoiceListVo.setVipList(list);

        return vipVoiceListVo;
    }

    /**
     * 官方问答往期列表
     *
     * @param token
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/offical/question/list/{page}-{pageSize}")
    public Object getOfficalQuestionList(Authentication token, @PathVariable("page") Integer page,
                                         @PathVariable("pageSize") Integer pageSize) {
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }


        List<OfficialQuestionVo> list = officialQuestionInfoService.getOfficialQuestionInfoList(page, pageSize);

        return list;
    }


    /**
     * 官方问答详情列表
     *
     * @param token
     * @param questionId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/offical/question/detail/{questionId}/{page}-{pageSize}")
    public Object getOfficalQuestionDetailList(Authentication token, @PathVariable("questionId") Integer questionId,
                                               @PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }

        OfficialQuestionVo obj = officialQuestionInfoService.getOfficialQuestionByQuestionId(questionId);

        List<OfficialQuestionToAnswerVo> list = officialQuestionToAnswerService.selectPageInfoByQuestionId(questionId,
                page, pageSize);

        OfficialQuestionDetailVo officialQuestionDetailVo = new OfficialQuestionDetailVo(obj, list);

        return officialQuestionDetailVo;
    }


    /**
     * 获取vip中语音地址
     *
     * @param token
     * @param questionId
     * @param answerUserId
     * @return
     */
    @GetMapping("/vip/voice/{questionId}/{answerUserId}")
    public Object getVipVoice(Authentication token, @PathVariable("questionId") Long questionId,
                              @PathVariable("answerUserId") Long answerUserId) {

        if (questionId == null || answerUserId == null) {
            throw new ConsumeRuntimeException("请求参数不合法");
        }
        Subject subject = (Subject) token.getPrincipal();

        //判断用户是否是Vip用户，如果用户是Vip 给语音播放地址,如果不是，提示前端此用户不是Vip用户
        UserCacheVo user = userInfoCache.getUserCacheInfo(subject.getId());

        if (user.getIsVip() !=1 && user.getIsSvip()!=1 && user.getIsPlatinumVip()!=1) {
            throw new ConsumeRuntimeException("非VIP用户不可访问");
        }

        String url = eavesdroppingService.getVipVoice(questionId, subject.getId());

        return new ResultVo(url);
    }

    /**
     * 查看秘密
     *
     * @param userId 问题所有者或回答者id
     * @return
     */
    @GetMapping("/secret/{userId}/{page}-{pageSize}")
    public Object getSecretVoiceList(@PathVariable("userId") Long userId,
                                     @PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {

        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }

        List<QuestionAndAnswerVo> list = eavesdroppingService.getSecretVoiceList(userId, page, pageSize);

        return list;
    }

    /**
     * 小秘密偷听
     *
     * @return
     */
    @GetMapping("/little/secret/{questionId}")
    public Object getLittleSecret(Authentication token, @PathVariable Long questionId) {
        Subject subject = (Subject) token.getPrincipal();

        if (questionId == null) {
            throw new ConsumeRuntimeException("请求参数不合法");
        }

        //查询用户是否购买过此条语音，购买过给语音地址，没购买过提示前端此用户要想偷听请支付5给金币。
        String url = eavesdroppingService.getLittleSecretUrl(subject.getId(), questionId);

        return new ResultVo(url);
    }


    /**
     * 问题列表
     *
     * @param token
     * @param type     1: 待回答，2：以回答，3：以提问
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/answer/list/{type}/{page}-{pageSize}")
    public Object getAnswerList(Authentication token, @PathVariable Integer type, @PathVariable("page") Integer page,
                                @PathVariable("pageSize") Integer pageSize) {
        Subject subject = (Subject) token.getPrincipal();
        if (type == null) {
            throw new IllegalParamException("类型不能为空");
        }

        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }

        long userId = subject.getId();

        List<QuestionAndAnswerVo> list = eavesdroppingService.selectListByAnchor(type, userId, page, pageSize);

        return list;
    }

    /**
     * 偷听价格查询
     *
     * @param type 1：非主播页面偷听，2：主播页面偷听
     * @return
     */
    @GetMapping("/answer/price/{type}")
    public Object getAnswerPrice(@PathVariable("type") Integer type) {

        Map<String, Object> priceMap = new HashMap<>(0);
        priceMap.put("payId", 1);
        priceMap.put("msg", "5");

        return priceMap;
    }


}
